$(function () {
    var count = 5;
    var timer = null;

    //发送短信验证码
    $('#send_code').click(function () {
        phone = $('#tel').val();
        $.ajax({
            type:'get',
            url:'/get_code/',
            data:'phone='+phone,
            success:function (msg) {
                console.log('Data Saved:'+msg);
                obj = eval('("+msg+")');
                console.log('结果：'+obj.Message);
            },

            error:function (res) {
                console.log(res.status)
            }
            });

        //倒计时功能
        $(this).attr('disable','true');
        var _this = $(this);
        timer = setInterval(function () {
            count--;
            console.log('剩余时间：'+count);
            if(count>=0){
                _this.val(count+'秒后重发')
            }
            else{
                _this.disable = false;
                clearInterval(timer);
                _this.val('重新发送');
                count = 5;
                $('#send_code').removeAttr('disable')
            }
        },1000);
    });

    //验证手机验证码
    $('#auth_code').change(function () {
        phone = $('#tel').val();
        code = $('#auth_code').val();
        $.ajax({
            type:'get',
            url:'/check_code/',
            data:'phone='+phone+'&code='+code,
            success:function (msg) {
                console.log('Data Saved:'+msg);
                alert(msg.code);
            },
            error:function (res) {
                console.log(res.status);
            }
        })
    });

})